package com.yanqu.road.dao.impl.activity.celebration;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.celebration.CelebrationCrossProgress;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/7/12 17:41
 */
public class CelebrationDataDaoImpl extends TempDataObjectDao<CelebrationCrossProgress> {

    public static String getCelebrationProgressSql = "select * from `t_c_activity_celebration_progress` where `activityId`=?";
    public Map<Integer, CelebrationCrossProgress> getCelebrationProgressMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,CelebrationCrossProgress> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCelebrationProgressSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CelebrationCrossProgress item = parse(rs);
                map.put(item.getId(),item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCelebrationProgressSql = "update `t_c_activity_celebration_progress` set score=? where activityId=? and id=?";
    @Override
    protected boolean updateData(CelebrationCrossProgress celebrationProgress) {
        celebrationProgress.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, celebrationProgress.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, celebrationProgress.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, celebrationProgress.getId()));
        if (getDb().executeNoneQuery(updateCelebrationProgressSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCelebrationProgressSql = "insert into `t_c_activity_celebration_progress`(activityId,id,score) values (?,?,?)";
    @Override
    protected boolean addData(CelebrationCrossProgress celebrationProgress) {
        celebrationProgress.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, celebrationProgress.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, celebrationProgress.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, celebrationProgress.getScore()));
        if (getDb().executeNoneQuery(insertCelebrationProgressSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CelebrationCrossProgress parseData(ResultSet rs) throws SQLException {
        CelebrationCrossProgress celebrationProgress = new CelebrationCrossProgress();
        celebrationProgress.setActivityId(rs.getInt("activityId"));
        celebrationProgress.setId(rs.getInt("id"));
        celebrationProgress.setScore(rs.getLong("score"));

        return celebrationProgress;
    }

}
